package com.ebupt.migu.music.commons.utils;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author liuyangyang@cd.ebupt.com
 * @Date 2020/8/12 23:04
 */
@Slf4j
@Service
public class SCPAsync {
    /**
     * ETL主机参数
     */
    @Value("${etl_server_ip}")
    private String ETL_SERVER_IP;
    @Value("${etl_server_name}")
    private String ETL_SERVER_NAME;
    @Value("${etl_server_password}")
    private String ETL_SERVER_PASSWORD;
    @Value("${etl_server_path}")
    private String ETL_SERVER_PATH;

    /**
     * 文件传输至ETL服务器
     *
     * @param localFile
     */
    @Async("strategy_executor")
    public void scpFile(String localFile) {
        log.info("上传至ETL服务器");
        Connection conn = new Connection(ETL_SERVER_IP);
        try {
            conn.connect();
            boolean isAuthenticated = conn.authenticateWithPassword(ETL_SERVER_NAME, ETL_SERVER_PASSWORD);
            if (isAuthenticated == false) {
                log.error("etl scp 登录验证失败");
            }
            log.info("登录验证成功");
            SCPClient client = new SCPClient(conn);
            client.put(localFile, ETL_SERVER_PATH);
            log.info("传输完成");
            conn.close();
        } catch (Exception e) {
            log.error("scp出错：{}", e.getMessage());
        }
    }
}
